import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'  // 使用我们的插件文件
import axios from 'axios'

Vue.config.productionTip = false

// 配置axios
axios.defaults.baseURL = process.env.VUE_APP_BASE_API || '/api'
axios.interceptors.request.use(config => {
  const token = store.state.user.token
  if (token) {
    config.headers['Authorization'] = `Bearer ${token}`
  }
  return config
}, error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  return response.data
}, error => {
  if (error.response) {
    switch (error.response.status) {
      case 401:
        store.dispatch('user/logout')
        router.push('/login')
        break
      case 403:
        ElementUI.Message.error('没有权限进行此操作')
        break
      case 500:
        ElementUI.Message.error('服务器错误')
        break
      default:
        ElementUI.Message.error(error.response.data.message || '请求失败')
    }
  }
  return Promise.reject(error)
})

Vue.prototype.$http = axios

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app') 